package main

import (
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"shop/database/mysql"
	"shop/internal/models"
	"shop/router"
	"time"
)

func main() {
	r := gin.Default()

	err := mysql.WithMysql(func(db *gorm.DB) error {
		return nil
	})
	if err != nil {
		return
	}

	err = models.AutoMigrate()
	if err != nil {
		panic(fmt.Errorf("automatic migration exception"))
	}
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"*"},
		AllowHeaders:     []string{"*"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "https://github.com"
		},
		MaxAge: 12 * time.Hour,
	}))
	router.Route(r)

	r.Run(":8999")
}
